package com.cw.jeeyt.service.impl.git;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cw.jeeyt.common.utils.MavenUtil;
import com.cw.jeeyt.common.utils.StringUtil;
import com.cw.jeeyt.service.bo.git.GitProjectBo;
import com.cw.jeeyt.service.bo.git.NewGitBranchBo;
import com.cw.jeeyt.core.intf.GitCore;
import com.cw.jeeyt.core.util.SessionUtil;
import com.cw.jeeyt.dao.intf.git.GitProjectDao;
import com.cw.jeeyt.dao.model.git.GitProjectDo;
import com.cw.jeeyt.dao.query.git.GitProjectQuery;
import com.cw.jeeyt.service.intf.git.GitProjectService;
import com.cw.jeeyt.service.converter.git.GitProjectConverter;
import com.cw.lang.mybatis.dto.PageInfo;
import com.cw.lang.mybatis.dto.PageParam;

/**
 * git项目信息接口业务实现类
 *
 * @author chenw
 * @date 2019-11-24 16:15:01
 */
@Service
public class GitProjectServiceImpl implements GitProjectService {
    @Autowired
    private GitProjectDao gitProjectDao;
    @Autowired
    private GitCore gitCore;
    private String LOCAL_REPO_PATH = "D:/chenw_project/output/";
    /**
     * 新增git项目信息
     *
     * @param request 条件
     * @return String
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addGitProject(GitProjectBo request) {
        if (request == null) {
            return false;
        }
        request.setCreateBy(SessionUtil.getSessionUserId());
        request.setUpdateBy(SessionUtil.getSessionUserId());
        request.setCreateDate(new Date());
        request.setUpdateDate(new Date());
        gitProjectDao.insertBySelective(GitProjectConverter.bo2Do(request));
        return true;
    }

    /**
     * 更新git项目信息
     *
     * @param request 条件
     * @return boolean
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean modifyGitProject(GitProjectBo request) {
        if (request == null) {
            return false;
        }
        request.setUpdateBy(SessionUtil.getSessionUserId());
        request.setUpdateDate(new Date());
        gitProjectDao.updateBySelective(GitProjectConverter.bo2Do(request));
        return true;
    }

    /**
     * 删除git项目信息
     *
     * @param request 条件
     * @return boolean
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteGitProject(GitProjectBo request) {
        if (request == null) {
            return false;
        }
        return gitProjectDao.deleteByKey(request.getId()) > 0;
    }

    /**
     * 分页查询git项目信息
     *
     * @param request 条件
     * @return PageInfo<GitProjectBo>
     */
    @Override
    public PageInfo<GitProjectBo> selectPage(GitProjectBo request, PageParam pageParam) throws Exception {
        PageInfo<GitProjectDo> pageInfo = gitProjectDao.selectPage(GitProjectQuery.builder()
            .id(request.getId())
            .projectCode(request.getProjectCode())
            .projectName(request.getProjectName())
            .localRepoPath(request.getLocalRepoPath())
            .remoteRepoUri(request.getRemoteRepoUri())
            .gitUsername(request.getGitUsername())
            .gitPassword(request.getGitPassword())
            .createBy(request.getCreateBy())
            .createDate(request.getCreateDate())
            .updateBy(request.getUpdateBy())
            .updateDate(request.getUpdateDate())
            .build(), pageParam);
        List<GitProjectBo> gitProjects = GitProjectConverter.doList2BoList(pageInfo.getList());
        return new PageInfo<>(pageInfo, gitProjects);
    }

    /**
     * 查询git项目信息详情信息
     *
     * @param request 条件
     * @return GitProjectBo
     */
    @Override
    public GitProjectBo queryGitProjectDetail(GitProjectBo request) {
        if (request == null) {
            return null;
        }
        return GitProjectConverter.do2Bo(gitProjectDao.selectByKey(request.getId()));
    }

    /**
     * 根据条件查询git项目信息
     *
     * @param request 条件
     * @return List<GitProjectBo>
     */
    @Override
    public List<GitProjectBo> queryGitProjectList(GitProjectBo request) {
        if (request == null) {
            return null;
        }
        return GitProjectConverter.doList2BoList(gitProjectDao.selectByExample(GitProjectQuery.builder()
            .id(request.getId())
            .projectCode(request.getProjectCode())
            .projectName(request.getProjectName())
            .localRepoPath(request.getLocalRepoPath())
            .remoteRepoUri(request.getRemoteRepoUri())
            .gitUsername(request.getGitUsername())
            .gitPassword(request.getGitPassword())
            .createBy(request.getCreateBy())
            .createDate(request.getCreateDate())
            .updateBy(request.getUpdateBy())
            .updateDate(request.getUpdateDate())
            .build()));
    }

    /**
     * 根据key查询git项目信息
     *
     * @param id
     * @return
     */
    @Override
    public GitProjectBo selectByKey(Long id) {

        return GitProjectConverter.do2Bo(gitProjectDao.selectByKey(id));
    }

    @Override
    public List<GitProjectBo> selectListByProjectIds(String projectIds) {
        String[] projectIdStrs = projectIds.split(",");
        List<Long> projectIdList = new ArrayList<>();
        for(String projectId : projectIdStrs){
            projectIdList.add(Long.valueOf(projectId));
        }
        return GitProjectConverter.doList2BoList(gitProjectDao.selectByExample(GitProjectQuery.builder().ids(projectIdList).build()));
    }

    /**
     * 拉取新的git分支
     *
     * @param newGitBranchBo
     * @return
     */
    @Override
    public boolean newBranch(NewGitBranchBo newGitBranchBo) {
        String projectIds = newGitBranchBo.getProjectIds();
        if(StringUtil.isEmpty(projectIds)){
            return false;
        }
        String [] projectIdList = projectIds.split(",");
        for(String projectId : projectIdList){
            GitProjectDo gitProject = gitProjectDao.selectByKey(Long.valueOf(projectId));
            //创建仓库，仅需要执行一次
            gitCore.setupRepository(gitProject.getProjectCode(),newGitBranchBo.getBaseBranchCode());
            //pull代码
            gitCore.pull(gitProject.getProjectCode(),newGitBranchBo.getBaseBranchCode());
            //新建分支
            gitCore.newBranch(gitProject.getProjectCode(),newGitBranchBo.getNewBranchCode());
            //更改版本号
            new MavenUtil().maven(LOCAL_REPO_PATH,gitProject.getProjectCode(),newGitBranchBo.getNewBranchCode());
            //commit and push
            gitCore.commitAndPush(LOCAL_REPO_PATH,"拉分支,更新版本");
        }
        return true;
    }
}
